#include "dashboard_view_model.h"
#include <QDebug>
#include <QTime>

namespace presentation {

DashboardViewModel::DashboardViewModel(QObject* parent)
    : BaseViewModel(parent)
    , m_refreshTimer(new QTimer(this))
{
    // 设置刷新定时器
    m_refreshTimer->setInterval(5000); // 5秒刷新一次
    connect(m_refreshTimer, &QTimer::timeout, this, &DashboardViewModel::onRefreshTimer);
    
    qDebug() << "DashboardViewModel: Created";
}

void DashboardViewModel::initialize()
{
    if (isInitialized()) {
        return;
    }

    qDebug() << "DashboardViewModel: Initializing";
    
    try {
        // 初始化仪表板数据
        updateSystemStatus();
        updateAgvCount();
        updateTaskCount();
        
        // 添加一些示例数据
        m_recentAlerts.append("System started successfully");
        m_recentAlerts.append("Map loaded: warehouse_floor_1");
        
        // 启动刷新定时器
        m_refreshTimer->start();
        
        // 调用基类初始化
        BaseViewModel::initialize();
        
        qDebug() << "DashboardViewModel: Initialization completed";
        
    } catch (const std::exception& e) {
        handleError(QString("DashboardViewModel initialization failed: %1").arg(e.what()));
    }
}

void DashboardViewModel::cleanup()
{
    if (!isInitialized()) {
        return;
    }

    qDebug() << "DashboardViewModel: Cleaning up";
    
    try {
        // 停止刷新定时器
        if (m_refreshTimer->isActive()) {
            m_refreshTimer->stop();
        }
        
        // 清理数据
        m_systemStatus.clear();
        m_activeAgvCount = 0;
        m_pendingTaskCount = 0;
        m_recentAlerts.clear();
        
        // 调用基类清理
        BaseViewModel::cleanup();
        
        qDebug() << "DashboardViewModel: Cleanup completed";
        
    } catch (const std::exception& e) {
        qWarning() << "DashboardViewModel: Exception during cleanup:" << e.what();
    }
}

void DashboardViewModel::refreshDashboard()
{
    qDebug() << "DashboardViewModel: Refreshing dashboard data";
    
    setLoading(true);
    
    try {
        updateSystemStatus();
        updateAgvCount();
        updateTaskCount();
        
        setLoading(false);
        
    } catch (const std::exception& e) {
        setLoading(false);
        handleError(QString("Failed to refresh dashboard: %1").arg(e.what()));
    }
}

void DashboardViewModel::clearAlerts()
{
    qDebug() << "DashboardViewModel: Clearing alerts";
    
    if (!m_recentAlerts.isEmpty()) {
        m_recentAlerts.clear();
        emit alertsChanged();
    }
}

void DashboardViewModel::addTestAlert()
{
    qDebug() << "DashboardViewModel: Adding test alert";
    
    QString testAlert = QString("Test alert - %1").arg(QTime::currentTime().toString());
    m_recentAlerts.prepend(testAlert);
    
    // 限制警报数量
    if (m_recentAlerts.size() > 10) {
        m_recentAlerts.removeLast();
    }
    
    emit alertsChanged();
}

void DashboardViewModel::onRefreshTimer()
{
    // 模拟数据更新
    updateAgvCount();
    updateTaskCount();
}

void DashboardViewModel::updateSystemStatus()
{
    QString newStatus = "Online"; // 这里应该从实际服务获取状态
    
    if (m_systemStatus != newStatus) {
        m_systemStatus = newStatus;
        emit systemStatusChanged();
    }
}

void DashboardViewModel::updateAgvCount()
{
    int newCount = 3; // 这里应该从实际服务获取AGV数量
    
    if (m_activeAgvCount != newCount) {
        m_activeAgvCount = newCount;
        emit agvCountChanged();
    }
}

void DashboardViewModel::updateTaskCount()
{
    int newCount = 5; // 这里应该从实际服务获取任务数量
    
    if (m_pendingTaskCount != newCount) {
        m_pendingTaskCount = newCount;
        emit taskCountChanged();
    }
}

} // namespace presentation
